package org.glut.cloud.repairManage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.glut.cloud.repairManage.config.QueryPageParam;
import org.glut.cloud.repairManage.config.Result;
import org.glut.cloud.repairManage.dto.RepairPeopleStateDto;
import org.glut.cloud.repairManage.dto.RepairPersonnelDto;
import org.glut.cloud.repairManage.entity.RepairPersonnel;
import org.glut.cloud.repairManage.service.IRepairPersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 维修人员表 前端控制器
 *
 * @since 2024-08-13 15:47
 */
@RestController
@RequestMapping("/repair-personnel")
public class RepairPersonnelController {

    @Autowired
    private IRepairPersonnelService repairPersonnelService;

    /**
     * 获取所有维修人员信息（过滤已删除记录）
     *
     * @return 所有维修人员信息
     */
    @GetMapping("/list")
    public List<RepairPersonnel> getAllRepairPersonnel() {
        LambdaQueryWrapper<RepairPersonnel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RepairPersonnel::getIsDelete, 0); // 过滤已删除记录
        return repairPersonnelService.list(queryWrapper);
    }

    /**
     * 新增维修人员
     *
     * @param repairPersonnel 维修人员信息
     */
    @PostMapping("/save")
    public boolean addRepairPersonnel(@RequestBody RepairPersonnel repairPersonnel) {
        return repairPersonnelService.save(repairPersonnel);
    }

    /**
     * 修改维修人员
     *
     * @param repairPersonnel 维修人员信息
     */
    @PostMapping("/mod")
    public boolean modRepairPersonnel(@RequestBody RepairPersonnel repairPersonnel) {
        return repairPersonnelService.updateById(repairPersonnel);
    }

    /**
     * 新增或修改维修人员
     *
     * @param repairPersonnel 维修人员信息
     */
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody RepairPersonnel repairPersonnel) {
        return repairPersonnelService.saveOrUpdate(repairPersonnel);
    }

    /**
     * 假删除维修人员 (软删除)
     *
     * @param id 维修人员ID
     */
    @GetMapping("/delete")
    public boolean deleteRepairPersonnel(@RequestParam("repair_personnel_id") Integer id) {
        RepairPersonnel repairPersonnel = new RepairPersonnel();
        repairPersonnel.setRepairPersonnelId(id);
        repairPersonnel.setIsDelete(1); // 设置删除标志为1
        return repairPersonnelService.updateById(repairPersonnel);
    }

    /**
     * 模糊查询维修人员 (过滤已删除记录)
     *
     * @param repairPersonnel 维修人员信息
     */
    @PostMapping("/ListP")
    public List<RepairPersonnel> listRepairPersonnel(@RequestBody RepairPersonnel repairPersonnel) {
        LambdaQueryWrapper<RepairPersonnel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(RepairPersonnel::getFullName, repairPersonnel.getFullName());
        lambdaQueryWrapper.eq(RepairPersonnel::getIsDelete, 0); // 过滤已删除记录
        // 可以根据需要增加更多的查询条件，例如性别、维护类型等
        return repairPersonnelService.list(lambdaQueryWrapper);
    }

    /**
     * 分页查询维修人员，下面这个方法是Ipage的分页查询方法,自定义，相关方法需要自己创建
     */
    @PostMapping("/listRepairPersonnelPage1")
    public Result listRepairPersonnelPage1(@RequestBody QueryPageParam query) {
        HashMap<String, Object> param = query.getParam();
        String username = (String) param.get("username");
        String gender = (String) param.get("gender");

        Page<RepairPersonnel> page = new Page<>(query.getPageNum(), query.getPageSize());

        LambdaQueryWrapper<RepairPersonnel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (username != null && !username.isEmpty()) {
            lambdaQueryWrapper.like(RepairPersonnel::getUsername, username);
        }
        if (gender != null && !gender.isEmpty()) {
            lambdaQueryWrapper.eq(RepairPersonnel::getGender, gender);
        }
        lambdaQueryWrapper.eq(RepairPersonnel::getIsDelete, 0); // 过滤已删除记录

        IPage<RepairPersonnel> result = repairPersonnelService.page2(page, lambdaQueryWrapper);
        System.out.println("total==" + result.getTotal());

        return Result.success(result.getRecords(), result.getTotal());
    }

    @GetMapping("/setId/{username}")
    public Integer getRepairPersonnelId(@PathVariable String username) {
        return repairPersonnelService.getRepairPersonnelId(username);
    }

    /**
     * 批量假删除维修人员 (软删除)
     *
     * @param repairPersonnelDto 维修人员信息
     */
    @PostMapping("/deleteBatch")
    public boolean deleteBatch(@RequestBody RepairPersonnelDto repairPersonnelDto) {
        List<Integer> ids = repairPersonnelDto.getRepairPersonnelIdList();
        return repairPersonnelService.fakeRemoveByIds(ids);
    }

    /**
     * 查看维修人员状态
     *
     * @param id 维修人员ID
     * @return 维修人员状态
     */
    @GetMapping("/state/{id}")
    public RepairPeopleStateDto getRepairPersonnelState(@PathVariable Integer id) {
        RepairPersonnel repairPersonnel = repairPersonnelService.getById(id);
        if (repairPersonnel != null && repairPersonnel.getIsDelete() == 0) {
            RepairPeopleStateDto stateDto = new RepairPeopleStateDto();
            stateDto.setRepairPersonnelId(repairPersonnel.getRepairPersonnelId());
            stateDto.setFullName(repairPersonnel.getFullName());
            stateDto.setMonthlyTaskCount(repairPersonnel.getMonthlyTaskCount());
            stateDto.setMonthlyTaskTarget(repairPersonnel.getMonthlyTaskTarget());
            stateDto.setTotalTaskCount(repairPersonnel.getTotalTaskCount());
            stateDto.setBusyStatus(repairPersonnel.getBusyStatus());
            return stateDto;
        }
        return null;
    }
}
